﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IsolatedStorageLogger.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the IsolatedStorageLogger type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.Logging
{
    #region Using Directives

    using System;
    using System.IO.IsolatedStorage;
    using StreetCareAPI;

    #endregion

    /// <summary>
    /// Defines the IsolatedStorageLogger type.
    /// </summary>
    public class IsolatedStorageLogger : IClientLogger
    {
        #region Constants

        /// <summary>
        /// Storage category format
        /// </summary>
        private const string StorageCategoryFormat = "LastEvent_{0}";

        /// <summary>
        /// Critical severity
        /// </summary>
        private const string SeverityCritical = "Critical";

        /// <summary>
        /// Error severity
        /// </summary>
        private const string SeverityError = "Error";

        /// <summary>
        /// Warning severity
        /// </summary>
        private const string SeverityWarning = "Warning";

        /// <summary>
        /// Information severity
        /// </summary>
        private const string SeverityInformation = "Information";

        #endregion

        #region Properties

        /// <summary>
        /// Isolated storage
        /// </summary>
        private static readonly IsolatedStorageSettings Storage = IsolatedStorageSettings.ApplicationSettings;

        #endregion

        #region Implement Interface IClientLogger

        /// <summary>
        /// Writes a critical event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteCritical(string message)
        {
            WriteStorage(SeverityCritical, message);
        }

        /// <summary>
        /// Writes a critical event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        public void WriteCritical(string message, Exception ex)
        {
            WriteStorage(SeverityCritical, message, ex);
        }

        /// <summary>
        /// Writes a critical event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        public void WriteCritical(string message, ServiceException ex)
        {
            WriteStorage(SeverityCritical, message, ex);
        }

        /// <summary>
        /// Writes an error event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteError(string message)
        {
            WriteStorage(SeverityError, message);
        }

        /// <summary>
        /// Writes an error event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        public void WriteError(string message, Exception ex)
        {
            WriteStorage(SeverityError, message, ex);
        }

        /// <summary>
        /// Writes an error event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        public void WriteError(string message, ServiceException ex)
        {
            WriteStorage(SeverityError, message, ex);
        }

        /// <summary>
        /// Writes an information event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteInfo(string message)
        {
            WriteStorage(SeverityInformation, message);
        }

        /// <summary>
        /// Writes a warning event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        public void WriteWarning(string message)
        {
            WriteStorage(SeverityWarning, message);
        }

        /// <summary>
        /// Writes a warning event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        public void WriteWarning(string message, Exception ex)
        {
            WriteStorage(SeverityWarning, message, ex);
        }

        /// <summary>
        /// Writes a warning event to the log.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        public void WriteWarning(string message, ServiceException ex)
        {
            WriteStorage(SeverityWarning, message, ex);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Writes the storage.
        /// </summary>
        /// <param name="severity">The severity.</param>
        /// <param name="message">The message.</param>
        private static void WriteStorage(string severity, string message)
        {
            var storageCategory = string.Format(StorageCategoryFormat, severity);
            var storageValue = message;

            WriteStorageValue(storageCategory, storageValue);
        }

        /// <summary>
        /// Writes the storage.
        /// </summary>
        /// <param name="severity">The severity.</param>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        private static void WriteStorage(string severity, string message, Exception ex)
        {
            var storageCategory = string.Format(StorageCategoryFormat, severity);
            var storageValue = message + " - " + (ex != null ? ex.ToString() : null);

            WriteStorageValue(storageCategory, storageValue);
        }

        /// <summary>
        /// Writes the storage.
        /// </summary>
        /// <param name="severity">The severity.</param>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        private static void WriteStorage(string severity, string message, ServiceException ex)
        {
            var storageCategory = string.Format(StorageCategoryFormat, severity);
            var storageValue = message + " - " + (ex != null ? ex.MessageStackTrace : null);

            WriteStorageValue(storageCategory, storageValue);
        }

        /// <summary>
        /// Writes the storage value.
        /// </summary>
        /// <param name="name">The name to write.</param>
        /// <param name="val">The value to write.</param>
        private static void WriteStorageValue(string name, string val)
        {
            if (!Storage.Contains(name))
            {
                Storage.Add(name, val);
            }
            else
            {
                Storage[name] = val;
            }

            Storage.Save();
        }

        #endregion
    }
}
